package cn.edu.hrbcu.everywhereapp.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

import cn.edu.hrbcu.everywhereapp.entity.BusLine;
import cn.edu.hrbcu.everywhereapp.entity.BusStop;

public class BusMapView extends View {
    private List<BusStop> busStops = new ArrayList<BusStop>();
    private BusLine busLine;
    private Paint paint;

    private int radius = 20;

    private int distance = 150;

    private int padding = 30;

    private int delta = 25;

    private int textSize = 50;

    public BusMapView(Context context, AttributeSet attributeSet) {
        super(context,attributeSet);
        init();
    }

    private void init(){
        paint = new Paint();
        paint.setARGB(255,88,172,34);
        paint.setStrokeWidth(10);
        paint.setTextSize(textSize);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        //drawBusLine(canvas,busLine);
        drawBusStops(canvas);
    }

    private void drawBusLine(Canvas canvas,BusLine line){
        float startX = distance;
        float startY = distance;
        float endX = distance;
        float endY = 2 * distance;

        // (1) 起点
        canvas.drawCircle(startX,startY,radius,paint);
        canvas.drawText(line.getStart(),startX + padding,startY + textSize / 3,paint);
        // (2) 中点
        for ( int i = 0 ;i < busLine.getStops().size();i++) {
            canvas.drawCircle(endX,endY,radius,paint);
            canvas.drawText(busLine.getStops().get(i),startX + padding,endY + textSize / 3,paint);
            canvas.drawLine(startX,startY + delta,endX,endY - delta,paint);

            startY += distance;
            endY += distance;
        }

        // (3) 终点
        canvas.drawLine(startX,startY + delta,endX,endY - delta,paint);
        canvas.drawCircle(endX,endY,radius,paint);
        canvas.drawText(line.getEnd(),startX + padding,endY + textSize / 3,paint);
    }

    private void drawBusStops(Canvas canvas){
        float startX = distance;
        float startY = distance;
        float endX = distance;
        float endY = 2 * distance;
        if(busStops.size() > 0){
            // (1) 起点
            BusStop busStop = busStops.get(0);
            canvas.drawCircle(startX,startY,radius,paint);
            canvas.drawText(busStop.getSitetime() + " " + busStop.getName(),startX + padding,startY + textSize / 3,paint);
            // (2) 中点
            for ( int i = 1 ;i < busStops.size() - 1;i++) {
                busStop = busStops.get(i);
                canvas.drawCircle(endX,endY,radius,paint);
                canvas.drawText(busStop.getSitetime() + " " +busStop.getName(),startX + padding,endY + textSize / 3,paint);
                canvas.drawLine(startX,startY + delta,endX,endY - delta,paint);

                startY += distance;
                endY += distance;
            }
            // (3) 终点
            busStop = busStops.get(busStops.size() - 1);
            canvas.drawLine(startX,startY + delta,endX,endY - delta,paint);
            canvas.drawCircle(endX,endY,radius,paint);
            canvas.drawText(busStop.getSitetime() + " " +busStop.getName(),startX + padding,endY + textSize / 3,paint);
        }
    }

    public void setBusLine(BusLine busLine){
        this.busLine = busLine;
    }

    public void setBusStops(List<BusStop> busStops){
        this.busStops = busStops;
    }
}
